var pages = pages || {};
(function(p) {
  p.common = {
    control: {
      reload: function() {
        window.location.reload(true);
      },
      goTo: function(url) {
        window.location.href = url;
      }
    },
    admin: {
      login: function() {
        window.location.href = "signin.asp";
      },
      logout: function() {
        $.ajax({
          url: "api.asp",
          type: "post",
          data: {
            "action": "logout"
          },
          success: function(data) {
            var oRet = pages.common.parseJSON(data);
            if (oRet) {
              if (oRet.ref == "ok") {
                window.location.href = "index.asp?" + Math.random();
              } else {
                alert("未知故障！登出失败");
              }
            } else {
              alert("未知故障！登出失败");
            }
          }
        });
      }
    },
    parseJSON: function(json) {
      var result = (new Function("return " + json))();
      return result;
    },
    includePack: function(option) {
      if (option instanceof Array) {
        for (var i = 0; i < option.length; i++) {
          if (option[i][1] == 'js') {
            _includeJS(option[i][0], option[i][2]);
          } else {
            _includeCSS(option[i][0], option[i][2]);
          }
        }
      }
    },
    json2html: function(json) {
      return _json_2_html(json);
    },
    resources: {
      bootstrap: [
        ['/css/bootstrap/css/bootstrap.css', 'css'],
        ['/css/bootstrap/js/bootstrap.min.js', 'js']

      ],
      codebeautify: [
        ['/js/codebeautify/xregexp-min.js', 'js', 'utf-8'],
        ['/js/codebeautify/shCore.js', 'js', 'utf-8'],
        ['/js/codebeautify/shAutoloader.js', 'js', 'utf-8'],
        ['/styles/shCoreDefault.css', 'css']
      ],
      fontawson: [
        ['/fontawson/css/font-awesome.css', 'css']
      ],
      globalResource: [
        ['/assets/css/pageshare.css', 'css'],
        ['/css/common.css', 'css']
      ],
      jqMD5: [
        ['/js/jQuery.md5.js', 'js']
      ]
    }
  };
  //local function
  function _json_2_html(structure_code) {
    structure_code['tagName'] = structure_code['tagName'] ? structure_code['tagName'] : 'div';
    structure_code['content'] = structure_code['content'] ? structure_code['content'] : '';
    structure_code['attributes'] = structure_code['attributes'] ? structure_code['attributes'] : [];
    structure_code['children'] = structure_code['children'] ? structure_code['children'] : [];
    var baseDom = document.createElement(structure_code.tagName);
    baseDom.innerHTML = structure_code.content;
    $(structure_code.attributes).each(function() {
      for (var key in this) {
        $(baseDom).attr(key, this[key])
      };
    });
    $(structure_code.children).each(function() {
      $(baseDom).append(_json_2_html(this));
    });
    return baseDom;
  }

  function _includeJS(path, encoding) {
    if (encoding && encoding.length > 0) {
      encoding = ' charset="utf-8"';
    } else {
      encoding = '';
    }
    document.write('<script type="text/javascript" src="' + path + '"' + encoding + '></script>');
  }

  function _includeCSS(path, encoding) {
    if (encoding && encoding.length > 0) {
      encoding = ' charset="utf-8"';
    } else {
      encoding = '';
    }
    document.write('<link type="text/css" href="' + path + '"' + encoding + ' rel="stylesheet" />');
  }
})(pages);
